package com.gosuncn.FileCopy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.PostConstruct;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 初始化派出所编号文件
 * @Author: zhangxiaolong
 * @Date: 2021/9/6 14:56
 */
@Configuration
public class InitPoliceCodeMap {
    private static final Logger log = LoggerFactory.getLogger(InitPoliceCodeMap.class);


    private static Map<String, String> map = new HashMap<>();
    private static final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    private Resource resource;




    public InitPoliceCodeMap(){
        Resource resource = resolver.getResource("classpath:PoliceStationCode");
        this.resource = resource;
    }

    public InitPoliceCodeMap(Resource resource){
        this.resource = resource;
    }

    @PostConstruct
    public void init(){
        InputStream is = null;
        BufferedInputStream bis = null;
        //暂存stringBuffer
        StringBuffer stringBuffer = new StringBuffer();
//        Resource resource = resolver.getResource("classpath:PoliceStationCode") ;
        try {
            is = this.resource.getInputStream();
            bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) !=-1){
                //将数据转为string类型
                String s = new String(buffer, 0, len);
                //写入stringBuffer
                stringBuffer.append(s);
            }
            //转string类型进行分割
            String s = stringBuffer.toString();
            String[] strings = s.split("\n");
            //遍历每一条数据
            for (String str : strings) {
                String[] str1 = str.split(",");
                //存入map
                map.put(str1[0],str1[1]);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        } finally {
            if (bis !=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is !=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     *获取初始化的派出所编号map
     * @author zxl
     * @date 2021/9/6
     * @return map
    */
    public static Map<String, String> getPoliceCodeMap()  {
        //当外部有资源文件需要覆盖
        Resource resource = resolver.getResource("file:PoliceStationCode756756");
        System.out.println(resource);
        InitPoliceCodeMap initPoliceCodeMap = new InitPoliceCodeMap(resource);

        //当资源文件存在 ture  不存在false
        if (resource.exists()){
            //重新执行init初始化，覆盖map
            initPoliceCodeMap.init();
        }
        return map;
    }

}
